package com.linlong.linlongchat.controller;


import com.linlong.linlongchat.domain.po.Message;
import com.linlong.linlongchat.domain.po.Result;
import com.linlong.linlongchat.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华少
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/message")
@Api(tags = "消息接口")
public class MessageController {

    @Autowired
    private IMessageService messageService;


    @PostMapping("/add/")
    @ApiOperation("添加消息")
    public Result addMessage(@RequestBody Message message) {
        message.setCreateTime(LocalDateTime.now());
        message.setUpdateTime(LocalDateTime.now());
        messageService.save(message);
        return Result.ok();
    }

    @GetMapping("/list/")
    @ApiOperation("根据会话id查询消息列表")
    public Result<List<Message>> getMessageByRecordId(@RequestParam Integer recordId) {
        List<Message> messageList =  messageService.getMessageByRecordId(recordId);
        return Result.ok(messageList);
    }

    @GetMapping("/count/{recordId}")
    @ApiOperation("根据会话id查询未读消息数量")
    public Result<Long> getCountByRecordId(@PathVariable Integer recordId) {
        Long count =  messageService.getCountByRecordId(recordId);
        return Result.ok(count);
    }

    @PutMapping("/updateStatus/{recordId}")
    @ApiOperation("根据会话id修改消息状态")
    public Result updateStatusByRecordId(@PathVariable Integer recordId) {
        messageService.updateStatusByRecordId(recordId);
        return Result.ok();
    }















}
